package com.jsj.seafile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsj.seafile.entity.UploadFileRes;
import com.jsj.seafile.realApi.realService;
import okhttp3.OkHttpClient;

import java.io.File;
import java.util.List;


public class ServiceImpl {
    public static final String SERVICE_URL = "http://124.222.166.200";
    public static final String FILE_UP_LINK="http://124.222.166.200:8000";
    public static final String FILE_UP_URL="http://124.222.166.200:8082";
    public static final String FILE_SERVER_ROOT="http://124.222.166.200/library/cb453a3b-156f-4bc3-8a5f-8eb5e7de31b7/testTran/";
    public realService realservice=new realService(SERVICE_URL,FILE_SERVER_ROOT,FILE_UP_URL,FILE_UP_LINK);
    OkHttpClient client = new OkHttpClient();
    public static String token="4b2261e84f2e4f22c77e2bafdd22f221a9f48f8a";
    String repo_id="bacc8aab-467c-4179-a77e-5b15b4d18c63";
    public  void updateToken(){
       token=realservice.obtainAuthToken(client,"563131295@qq.com","563131");
    } 
    public boolean realupload(String path, File... files){
        String uploadlink=realservice.getUploadLink(client,token,repo_id,path);
        System.out.println("p="+path+"   result="+uploadlink);
        JSONObject jsonObject=JSONObject.parseObject(uploadlink);
        String newtoken=jsonObject.getString("token");
        String uploadF=realservice.getUploadF(client,newtoken);
        //System.out.println(uploadF);
        jsonObject=JSONObject.parseObject(uploadF);
        uploadlink=jsonObject.getString("upload_link");
        if(files.length>0){
            List<UploadFileRes> uploadFileResList=realservice.uploadFile(client,token,uploadlink,path,"",files);
        }
        return true;
    }
    public boolean realDownload(String path, String savepath){
        String downlink=realservice.getDownLoadLink(client,token,repo_id,path);
        System.out.println(downlink);
        if(realservice.realDownload(client,downlink,savepath))
            System.out.println("sucess");
        return true;
    }
    public String GetDownLink(String path){
        return realservice.getDownLoadLink(client,token,repo_id,path);
    }


    /**
     * 检测文件夹是否存在
     */
    public boolean getDetailDir(String path){
        return realservice.getDirDeatil(client,token,repo_id,path);
    }


    public String peiheTest(int id){
        System.out.println("命中");
        return "1";
    }

    public String testZipToken(String p,List<String> dirents){
        String DirLink=realservice.getZipDownLink(client,token,p,repo_id);
        if(DirLink.length()<10){
            DirLink=realservice.CreateZipDownLink(client,token,p,repo_id);
        }
        JSONArray objects = JSONArray.parseArray(DirLink);
        JSONObject jsonObject = objects.getJSONObject(0);
        String DirToken=jsonObject.getString("token");
        System.out.println(DirToken);
        String zipToken=realservice.getDirDownloadToken(client,token,repo_id,"/",DirToken,dirents);


        return zipToken;
    }

    public String getRealZipLink(String ziptoken){
        String realzipLink=realservice.getRealZipLink(client,token,ziptoken);
        if(realzipLink==null)
            return "not complete";
        return realzipLink;
    }



    /**
     * 创建文件夹
     */
    public void testCreatDir(String path){
       if(realservice.createNewDir(client,token,repo_id,path))
           System.out.println("sucess");
    }
   /* @GetMapping("/testcache")
    @Cacheable(value = "emps",key="#id")
    public String getdata(int id){
        System.out.println("命中");
        return "1";
    }*/
}
